class CreateHospiceBranches < ActiveRecord::Migration
  def self.up
    create_table :hospice_branches do |t|
      t.integer :hospice_id
      t.string :branch_name, :limit => 50, :null=>false

      t.timestamps
    end
    
    add_index :hospice_branches, :hospice_id
    
    hospice = Hospice.find(:first)
    HospiceBranch.create(:hospice=>hospice, :branch_name=>"Port Elizabeth")
    HospiceBranch.create(:hospice=>hospice, :branch_name=>"Uitenhage")
    HospiceBranch.create(:hospice=>hospice, :branch_name=>"Motherwell")
    HospiceBranch.create(:hospice=>hospice, :branch_name=>"Kouga")
    HospiceBranch.create(:hospice=>hospice, :branch_name=>"Kwazakhele")
    HospiceBranch.create(:hospice=>hospice, :branch_name=>"New Brighton")
    HospiceBranch.create(:hospice=>hospice, :branch_name=>"Walmer Township")
    
    #Kill all our patients... we're going to be importing a lot later on
    Patient.destroy_all

    #also, need to drop hospice_id from patients table, and update all records to have a hospice_branch_id
    remove_index :patients, :hospice_id
    remove_column :patients, :hospice_id
  end

  def self.down
    
    add_column :patients, :hospice_id, :integer, :null => false
    Patient.find(:all).each do |patient|
      patient.hospice_id = Hospice.find(:first).id
      patient.save
    end
    add_index :patients, :hospice_id
    
    remove_index :hospice_branches, :hospice_id
    drop_table :hospice_branches
  end
end
